package com.company;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class Main {
     static Scanner sc=new Scanner(System.in);
     static StudentDAOImpl studentDAO=new StudentDAOImpl();
    public static void main(String[] args) {

        Menu menu = new Menu();

        while (true){
            menu.welcom();
            int nextInt = sc.nextInt();
            switch (nextInt){
                case -1:
                    menu.bye();
                    System.out.println();
                    break;
                case 0:
                    System.out.println("无法回到上一层");
                    break;
                case 1:
                    menu.selectmenu();
                    int nextInt1 = sc.nextInt();
                    switch (nextInt1){
                        case -1:
                            menu.bye();
                            System.out.println();
                            break;
                        case 0:
                            break;
                        case 1:
                            searchAll();
                            break;
                        case 2:
                            while (true){
                                menu.serachmenu();
                                int nextInt2 = sc.nextInt();
                                switch (nextInt2){
                                    case -1:
                                        menu.bye();
                                        System.out.println();
                                        break;
                                    case 0:
                                        break;
                                    case 1:
                                        searchbysID();
                                        break;
                                    case 2:
                                        searchbyName();
                                        break;
                                    case 3:
                                        searchbyCs();
                                        break;
                                    case 4:
                                        searchbyMs();
                                        break;
                                    case 5:
                                        searchbyEs();
                                        break;
                                    default:
                                        System.out.println("错误命令 无法实行");
                                        break;


                                }
                                if (nextInt2==0){
                                    break;
                                }
                            }
                            break;

                        default:
                            System.out.println("没有该功能！");
                            break;

                    }
                    break;
                case 2:
                    insert();
                    break;
                case 3:
                    upData();
                    break;
                case 4:
                    deleteData();
                    break;
                default :
                    System.out.println("错误命令 无法实行");
                    break;


            }

        }

    }

    private static void searchbyName() {
        System.out.println("请输入你要查找的学生姓名：");
        String name = sc.next();
        Connection connection = null;
        try {
            connection = Jdbcutil.getconnection2();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            List<Student> list = studentDAO.searchbyName(connection, name);
            System.out.println("结果如下：");
            for (Student item :
                    list) {
                System.out.println(item);
            }
        } catch (SQLException e) {
            System.out.println("查无此人");
        }

        Jdbcutil.closeResource(connection);
    }

    private static void searchbyEs() {
        System.out.println("请输入你要查找的学生英语成绩：");
        int es = sc.nextInt();
        Connection connection = null;
        try {
            connection = Jdbcutil.getconnection2();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            List<Student> list = studentDAO.searchbyengliss(connection, es);
            System.out.println("结果如下：");
            for (Student item :
                    list) {
                System.out.println(item);
            }
        } catch (SQLException e) {
            System.out.println("查无此人");
        }

        Jdbcutil.closeResource(connection);
    }

    private static void searchbyMs() {
        System.out.println("请输入你要查找的学生数学成绩：");
        int ms = sc.nextInt();
        Connection connection = null;
        try {
            connection = Jdbcutil.getconnection2();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            List<Student> list = studentDAO.searchbymaths(connection, ms);
            System.out.println("结果如下：");
            for (Student item :
                    list) {
                System.out.println(item);
            }
        } catch (SQLException e) {
            System.out.println("查无此人");
        }

        Jdbcutil.closeResource(connection);
    }

    private static void searchbyCs() {
        System.out.println("请输入你要查找的学生语文成绩：");
        int cs = sc.nextInt();
        Connection connection = null;
        try {
            connection = Jdbcutil.getconnection2();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            List<Student> list = studentDAO.searchbychineses(connection, cs);
            System.out.println("结果如下：");
            for (Student item :
                    list) {
                System.out.println(item);
            }
        } catch (SQLException e) {
            System.out.println("查无此人");
        }

        Jdbcutil.closeResource(connection);
    }

    private static void searchbysID() {
        System.out.println("请输入你要查找的学生id：");
        int sid = sc.nextInt();
        Connection connection = null;
        try {
            connection = Jdbcutil.getconnection2();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            Student student = studentDAO.searchbySId(connection, sid);
            System.out.println("结果如下：");
            System.out.println(student);
        } catch (SQLException e) {
            System.out.println("查无此人");
        }

        Jdbcutil.closeResource(connection);
    }

//

    private static void deleteData() {
        System.out.println("请输入要修改学生的id：");
        int id = sc.nextInt();
        Connection connection = null;
        try {
            connection = Jdbcutil.getconnection2();
            Student student = studentDAO.searchbyID(connection, id);
            System.out.println(student);
            System.out.println("请检查是否要该同学（y/n）");
            String next = sc.next();
            if ("y".equals(next)){
                studentDAO.deleteData(connection,student.getId());
            }
        } catch (SQLException e) {
            System.out.println("删除失败");
        } finally {
            Jdbcutil.closeResource(connection);
        }
    }

    private static void upData() {
        System.out.println("请输入要修改学生的id：");
        int id = sc.nextInt();
        Connection connection = null;
        try {
            connection = Jdbcutil.getconnection2();
            Student student = studentDAO.searchbyID(connection, id);
            System.out.println(student);
            System.out.println("请检查是否要该同学（y/n）");
            String next = sc.next();
            if ("y".equals(next)){
                System.out.println("请输入学生的新id：");
                int id1 = sc.nextInt();
                System.out.println("请输入学生的新姓名：");
                String name = sc.next();
                System.out.println("请输入学生的新语文成绩：");
                int cs = sc.nextInt();
                System.out.println("请输入学生的新英语成绩：");
                int es = sc.nextInt();
                System.out.println("请输入学生的新数学成绩：");
                int ms = sc.nextInt();
                Student student1=new Student(id1,name,cs,es,ms,cs+es+ms);
                studentDAO.updata(connection,student1);
            }
        } catch (SQLException e) {
            System.out.println("修改失败");
        } finally {
            Jdbcutil.closeResource(connection);
        }
    }

    private static void searchAll() {
        System.out.println("\t所有学生成绩如下");
        Connection connection = null;
        try {
            connection = Jdbcutil.getconnection2();
            List<Student> list = studentDAO.searchall(connection);
            for (Student item:
                 list) {
                System.out.println(item);
            }
        } catch (SQLException e) {
            System.out.println("查无此人");
        } finally {
            Jdbcutil.closeResource(connection);
        }
    }

    private static void insert() {
        System.out.println("请输入学生的id：");
        int id = sc.nextInt();

        System.out.println("请输入学生的姓名：");
        String name = sc.next();
        System.out.println("请输入学生的语文成绩：");
        int cs = sc.nextInt();
        System.out.println("请输入学生的英语成绩：");
        int es = sc.nextInt();
        System.out.println("请输入学生的数学成绩：");
        int ms = sc.nextInt();
        Student student=new Student(id,name,cs,es,ms,cs+es+ms);
        Connection connection = null;
        try {
            connection = Jdbcutil.getconnection2();
            studentDAO.insertdata(connection,student);
            System.out.println("添加成功！");
        } catch (SQLException e) {
            System.out.println("添加失败");
        } finally {
            Jdbcutil.closeResource(connection);
        }
    }
}
